package com.lepu.app.fun.chat.services;

import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;

import com.core.lib.utils.main.LogUtilBase;
import com.lepu.app.fun.chat.bean.Media;
import com.lepu.app.fun.chat.core.MediaManager;
import com.lepu.app.fun.chat.util.Constants;


/**
 * Created by Chang on 12/24/2014.
 */
public class MediaService extends IntentService {
    private static final String TAG = MediaService.class.getSimpleName();
    public static final String ACTION_UPLOAD_MEDIA = "media_upload";
    public static final String ACTION_DOWNLOAD_MEDIA = "media_download";

    public static final String LOCAL_BROADCASTING = "localBroadcast";

    public MediaService() {
        super("MediaService()");
        // in case we’re shut down unexpectedly
        setIntentRedelivery(false);
    }

    public MediaService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        String action = intent.getAction();
        LogUtilBase.LogE(TAG, "intent action = " + intent.getAction());

        Bundle data = intent.getExtras();
        if (data == null) {
            LogUtilBase.LogE(TAG, "onHandleIntent data is null");
            return;
        }

        Media media = (Media) data.getSerializable(Constants.Chat.CHAT_MEDIA);
        if (media == null) {
            LogUtilBase.LogE(TAG, "onHandleIntent media is null");
            return;
        }

        if (action.equals(ACTION_UPLOAD_MEDIA)) {
            doUpload(media);
        } else if (action.equals(ACTION_DOWNLOAD_MEDIA)) {
            doDownload(media);
        }
//        Intent resultsIntent = new Intent(LOCAL_BROADCASTING);
//        Bundle result = new Bundle();
//        result.putSerializable("media", media);
//        resultsIntent.putExtras(result);
//        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
//        localBroadcastManager.sendBroadcast(resultsIntent);
    }


    void doUpload(Media media) {
        if (media == null) {
            LogUtilBase.LogE(TAG, "onHandleIntent media is null");
            return;
        }
        if (media.getMessageType() == Constants.Chat.CHAT_MESSAGE_TYPE_IMAGE)
            MediaManager.getInstance().upload(media);
        else if (media.getMessageType() == Constants.Chat.CHAT_MESSAGE_TYPE_AUDIO)
            MediaManager.getInstance().upload(media);
        else if (media.getMessageType() == Constants.Chat.CHAT_MESSAGE_TYPE_VIDEO)
            MediaManager.getInstance().upload(media);
    }

    void doDownload(Media media) {
        if (media == null) {
            LogUtilBase.LogE(TAG, "onHandleIntent media is null");
            return;
        }
        if (media.getMessageType() == Constants.Chat.CHAT_MESSAGE_TYPE_IMAGE)
            MediaManager.getInstance().download(media);
        else if (media.getMessageType() == Constants.Chat.CHAT_MESSAGE_TYPE_AUDIO)
            MediaManager.getInstance().download(media);
        else if (media.getMessageType() == Constants.Chat.CHAT_MESSAGE_TYPE_VIDEO)
            MediaManager.getInstance().download(media);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtilBase.LogE(TAG, "MediaService is destoried!");

    }
}
